package com.example.pcdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.pcdemo.adpter.MainRecyclerViewAdapter;
import com.example.pcdemo.model.Items;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mainRecycler;
    private Button mainAdd,mainDelete;
    private MainRecyclerViewAdapter adapter;
    private List<Items> list = new ArrayList<>();

    private List<String>  titleStr=new ArrayList<>();

    private List<Integer> imgList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        addImg();
        addTitle();
        initRecyclerView();
    }



    private void initRecyclerView() {
        for (int i = 0; i < 15; i++) {
            int index = new Random().nextInt(imgList.size());
            list.add(new Items(imgList.get(index),titleStr.get(index)));
            titleStr.remove(index);
            imgList.remove(index);
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        mainRecycler = (RecyclerView) findViewById(R.id.main_recycler);
        mainAdd = (Button) findViewById(R.id.main_add);
        mainDelete = (Button) findViewById(R.id.main_delete);
        adapter = new MainRecyclerViewAdapter(this,list);
        mainRecycler.setLayoutManager(new LinearLayoutManager(this));
        mainRecycler.setAdapter(adapter);
        mainDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() == 0){
                    Toast.makeText(MainActivity.this, "没有数据了", Toast.LENGTH_SHORT).show();
                    //删除到没有数据后重新初始化图片和标题数据
                    addImg();
                    addTitle();
                }else if (list.size() == 1){
                    list.remove(0);
                }else{
                    list.remove(1);
                }
                adapter.notifyDataSetChanged();
            }
        });
        mainAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() <=20){
                    int index = new Random().nextInt(imgList.size());
                    if (list.size() < 3){
                        list.add(new Items(imgList.get(index),titleStr.get(index)));
                    }else {
                        list.add(2,new Items(imgList.get(index),titleStr.get(index)));
                    }
                    adapter.notifyDataSetChanged();
                }else {
                    Toast.makeText(MainActivity.this, "已满20行不能添加数据", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    private void addTitle() {
        Collections.addAll(titleStr,"Agent 51 - Bad Times (Album Version)",
                "Agent 51 - Been So Long (Album Version)",
                "Ariana Grande、MAC MILLER - Into You (Alex Ghenea Remix)",
                "Avril Lavigne - 17",
                "BEYOND - Amani (粤语)",
                "Bloc Party - Banquet",
                "Bon Jovi - Because We Can",
                "Coldplay - A Message (2005)",
                "Dream Theater - Another Day",
                "Ester drang - All The Feeling",
                "FLOW (フロウ) - 7-seven- (TVアニメ 「七つの大罪」 エンディングテーマ)",
                "Glen Check - 60's Cardin",
                "Green Day - 21 Guns (《变形金刚2》电影插曲)",
                "Groove Coverage - She.flac",
                "Halestorm - Bad Girl's World",
                "James Blunt - You're Beautiful - 2006重制版",
                "Jeff Buckley - Back In N.Y.C.",
                "Joshua Radin - Beautiful Day",
                "Lady Gaga - Poker Face",
                "Matt Nathanson - All We Are",
                "Miracle Mile - Beginagain",
                "Nickelback - Animals",
                "Oh Sunshine - beautiful (japanese ver.)",
                "OneRepublic - All The Right Moves",
                "OneRepublic - Apologize",
                "Paramore - Ain't It Fun",
                "Rise Against - Behind Closed Doors",
                "Scorpions - Always Somewhere",
                "Skid Row - 18 And Life",
                "Suede - Beautiful Ones",
                "TFBOYS - 开学第一课 (《开学第一课》节目主题曲)",
                "TFBOYS - 剩下的盛夏",
                "The Cranberries - Animal instinct",
                "The Dunwells - Animal",
                "The Fray - Be Still",
                "The Pretty Reckless - Just Tonight",
                "The Rolling Stones - (I Can't Get No) Satisfaction",
                "This Is Love-周兴哲");
    }
    private void addImg() {
        imgList.add(R.drawable.thumb01);
        imgList.add(R.drawable.thumb02);
        imgList.add(R.drawable.thumb03);
        imgList.add(R.drawable.thumb04);
        imgList.add(R.drawable.thumb05);
        imgList.add(R.drawable.thumb06);
        imgList.add(R.drawable.thumb07);
        imgList.add(R.drawable.thumb08);
        imgList.add(R.drawable.thumb09);
        imgList.add(R.drawable.thumb10);
        imgList.add(R.drawable.thumb11);
        imgList.add(R.drawable.thumb12);
        imgList.add(R.drawable.thumb13);
        imgList.add(R.drawable.thumb14);
        imgList.add(R.drawable.thumb15);
        imgList.add(R.drawable.thumb16);
        imgList.add(R.drawable.thumb17);
        imgList.add(R.drawable.thumb18);
        imgList.add(R.drawable.thumb19);
        imgList.add(R.drawable.thumb20);
        imgList.add(R.drawable.thumb21);
        imgList.add(R.drawable.thumb22);
        imgList.add(R.drawable.thumb23);
        imgList.add(R.drawable.thumb24);
        imgList.add(R.drawable.thumb25);
    }
}